<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts' // echarts theme
import resize from '@/framework/utils/mixins/resize'

require('echarts/theme/macarons')

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        title: {
          text: '各职位用户占比',
          x: 'left',
          y: 'top',
          textStyle: {
            color: '#131314',
            fontSize: '16',
            fontWeight: '500'
          }
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        // 图例
        legend: {
          left: 'center',
          icon: 'circle',
          bottom: '0',
          textStyle: {
            fontSize: 12
          },
          data: ['职位1', '职位2', '职位3', '职位4', '职位5']
        },
        color: ['#249EFF', '#836BCE', '#21CCFF', '#86DF6C', '#3461FF'],
        series: [
          {
            name: '职位占比',
            type: 'pie',
            // roseType: 'radius',
            radius: [50, 75],
            center: ['50%', '45%'],
            data: [
              { value: 320, name: '职位1' },
              { value: 240, name: '职位2' },
              { value: 149, name: '职位3' },
              { value: 100, name: '职位4' },
              { value: 59, name: '职位5' }
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600,
            emphasis: {
              label: {
                show: true,
                fontSize: 14,
                color: '#000',
                fontWeight: 'bold'
              }
            },
            label: {
              show: true,
              formatter: '{b} \n{d}%, {c}人',
              textStyle: {
                color: '#606266'
              }
            }
            // labelLine: {
            //   normal: {
            //     lineStyle: {
            //       color: '#606266'
            //     }
            //   }
            // }
          }
        ]
      })

      // 使用刚指定的配置项和数据显示图表。
    //   let index = 0// 默认选中第二个
    //   const myChart = this.chart // 解决echarts中this指向问题
    //   myChart.dispatchAction({
    //     type: 'highlight',
    //     seriesIndex: 0,
    //     dataIndex: 0 // 默认选中第二个
    //   })
    //   myChart.on('mouseover', function(e) {
    //     if (e.dataIndex !== index) {
    //       myChart.dispatchAction({
    //         type: 'downplay',
    //         seriesIndex: 0,
    //         dataIndex: index
    //       })
    //     }
    //   })
    //   myChart.on('mouseout', function(e) {
    //     index = e.dataIndex
    //     myChart.dispatchAction({
    //       type: 'highlight',
    //       seriesIndex: 0,
    //       dataIndex: e.dataIndex
    //     })
    //   })
    }
  }
}
</script>
